package com.ymatou.iapi.productmanager.compare;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.productmanager.parameter.CatalogPropertyDto;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogDto;
import com.ymatou.iapi.productmanager.parameter.ProductCatalogListBean;
import com.ymatou.iapi.productmanager.parameter.QuerySellerProductListResponseBean;
import com.ymatou.iapi.productmanager.parameter.productdetail.ProductAndCatalogInfoBean;

public class BeanHelper {
	public static QuerySellerProductListResponseBean oldQuerySellerProductListResponseBeanHandler(JSONObject jsonBean,QuerySellerProductListResponseBean responseBean){
		return responseBean;
		
	}
	
	public static ProductCatalogListBean oldProductCatalogListBeanHandler(JSONObject jsonBean,ProductCatalogListBean productCatalogListBean){
		try {
			JSONArray productCatalogInfos = jsonBean.getJSONArray("ProductCatalogInfos");
			for(int i=0;i<productCatalogInfos.length();i++){
				JSONObject productCatalogInfo = productCatalogInfos.getJSONObject(i);
				if (!productCatalogInfo.isNull("PropertyValues")&&productCatalogInfo.getJSONArray("PropertyValues").length()>0){
					String catalogId = productCatalogInfo.getString("sCatalogId");
					
					ProductCatalogDto productCatalogDto = productCatalogListBean.getProductCatalogInfos().stream()
														.filter(x->x.getCatalogId().equals(catalogId)).findFirst().orElse(null);
					
					
					if (productCatalogDto!=null){
						JSONArray propertyValues = productCatalogInfo.getJSONArray("PropertyValues");
						for(int j=0;j<propertyValues.length();j++){
							String key = propertyValues.getJSONObject(j).getString("sProperty");
							String val = propertyValues.getJSONObject(j).getJSONArray("PropertyValues")
														.getJSONObject(0).getString("sPropertyValue");
							
							
							CatalogPropertyDto catalogPropertyDto = productCatalogDto.getPropertyList().stream().filter(y->y.getName().equals(key)).findFirst().orElse(null);
							if (catalogPropertyDto!=null){
								catalogPropertyDto.setValue(val);
								
								if (propertyValues.getJSONObject(j).getJSONArray("PropertyValues")
										.getJSONObject(0).isNull("sPicUrl")){
									catalogPropertyDto.setPic("");
								}else{
									catalogPropertyDto.setPic(propertyValues.getJSONObject(j).getJSONArray("PropertyValues")
											.getJSONObject(0).getString("sPicUrl"));
								}
								
							}
						}
					}
					
				}
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return productCatalogListBean;
		
	}


	public static ProductAndCatalogInfoBean oldProductAndCatalogInfoBeanHandler(JSONObject jsonBean,ProductAndCatalogInfoBean productAndCatalogInfoBean){
		if (productAndCatalogInfoBean.getProductAndCatalogInfoOnSellerProduct()!=null){
			productAndCatalogInfoBean.getProductAndCatalogInfoOnSellerProduct()
							.setsBrandName(productAndCatalogInfoBean.getProductAndCatalogInfoOnSellerProduct().getsBrandEnName());
		}
		
		
		return productAndCatalogInfoBean;
	}

}
